
#import "AnimalView.h"
#import <appkit/NXBitmapImageRep.h>
#import <appkit/Control.h>
#import <sys/param.h>

/* 
 * AnimalView.m, a simple view to display an NXBitmapImageRep.  
 * Subclass of view that draws an NXBitmapImageRep.  Knows how 
 * to flip and rotate itself in response to IB controls.	
 * NXBitmapImageRep provides the functionality to deal with images 
 * that are stored as bitmaps.  The instances of this class are created by 
 * loading TIFF data.  In 1.0, I would have read bitmap data (from TIFF 
 * file or Mach-O) and imaged it using NXImageBitmap(). It works out much 
 * cleaner using NXBitmapImageRep.
 *
 * Author: Julie Zelenski, NeXT Developer Support
 * You may freely copy, distribute and reuse the code in this example.  
 * NeXT disclaims any warranty of any kind, expressed or implied, as to 
 * its fitness for any particular use.
 */
 
@implementation AnimalView:View

/* INIT/FREE METHODS */

- initFrame:(NXRect *)frameRect;
/* Init method for newly created animal view. It initializes the scaling
 * factors to normal, and then grabs the duck for the initial image.
 */
{
    [super initFrame:frameRect];
    scaleFactor.x = scaleFactor.y = 1.0;
    [self setImageToAnimal:"Duck"];
    return self;
}
   
- free;
/* Free the image on our way out.
 */
{
    [image free];
    return [super free];
}


/* TARGET/ACTION METHODS */

- rotateLeft:sender
/* Rotates the view 90 degrees to the left and redisplays.
 */
{  
    [self rotate:(scaleFactor.x*scaleFactor.y)*90.0];
    [self display];
    return self;
}

- rotateRight:sender
/* Rotates the view 90 degrees to the right and redisplays.
 */
{
    [self rotate:(scaleFactor.x*scaleFactor.y)*-90.0];
    [self display];
    return self;
}

- flipHorizontal:sender
/* Mirrors the view across vertical axis, and updates scale factor to 
 * indicate that.  Since rotation can change what is currently vertical, 
 * need to rotate to zero, do the scaling, and restore rotation.
 */ 
{   
    float angle;
    
    angle = [self boundsAngle];
    scaleFactor.x *= -1;
    [self rotate:-1*angle];
    [self scale:-1.0 :1.0];
    [self rotate:angle];
    [self display];
    return self;
}

- flipVertical:sender
/* Mirrors the view across horizontal axis, and updates scale factor to 
 * indicate that.  Since rotation can change what is currently horizontal, 
 * need to rotate to zero, do the scaling, and restore rotation.
 */ 
{
    float angle;
    
    angle = [self boundsAngle];
    scaleFactor.y *= -1;
    [self rotate:-1*angle];
    [self scale:1.0 :-1.0];
    [self rotate:angle];
    [self display];
    return self;
}

- changeAnimal:sender
/* Target/Action for IB control.  Changes to display image with name of
 * cell title.
 */
{   
    [self setImageToAnimal:[[sender selectedCell] title]];
    return self;
}



/* PRIVATE METHODS */

- setImageToAnimal:(const char *)imageName;
/* Find the bitmap image rep for the specified name, sets the scaling
 * and rotation back to "normal" and displays new image.
 */
{   char filename[MAXPATHLEN];

    sprintf(filename,"%s.tiff",imageName);
    if (image) [image free];
    image = [[NXBitmapImageRep alloc] initFromSection:filename];
    [self setDrawRotation:0.0];
    [self scale:scaleFactor.x :scaleFactor.y];
    scaleFactor.x = scaleFactor.y = 1.0;
    [self display];
    return self;
}

- drawSelf:(NXRect *)rects :(int)rectCount;
/* Clears the background and has the image draw itself 
 */
{
   NXEraseRect(&bounds);  /* to be sure to clear background */
   [image drawIn:&bounds];
   return self;
}

@end